package com.school.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.school.dao.HljtAuthorityManagementDao;
import com.school.entity.web.HljtAuthorityManagement;
import com.school.service.HljtAuthorityManagementService;

@Service
public class HljtAuthorityManagementServiceIpml implements HljtAuthorityManagementService{
	
	@Autowired
	private HljtAuthorityManagementDao hljtAuthorityManagementDao;

	@Override
	@Transactional(rollbackFor=Exception.class)
	public Integer addTeacherAuthority(HljtAuthorityManagement hljtAuthorityManagement) {
		hljtAuthorityManagementDao.addTeacher(hljtAuthorityManagement);
		if(hljtAuthorityManagement.getRole()=="最高权限") {
			hljtAuthorityManagement.setRoleid(3);
		}else if(hljtAuthorityManagement.getRole()=="班主任权限"){
			hljtAuthorityManagement.setRoleid(2);
		}else if(hljtAuthorityManagement.getRole()=="普通权限"){
			hljtAuthorityManagement.setRoleid(1);
		}
		hljtAuthorityManagementDao.addRole(hljtAuthorityManagement);
		if(null !=hljtAuthorityManagement.getTeacherStrengthSort()&&hljtAuthorityManagement.isTeacherExhibition()) {
			hljtAuthorityManagementDao.addExcellent(hljtAuthorityManagement);
		}
		hljtAuthorityManagementDao.addUser(hljtAuthorityManagement);
		return hljtAuthorityManagement.getTeacherId();
	}

	@Override
	@Transactional(readOnly=true)
	public HljtAuthorityManagement authorityManagementByTeacherId(HljtAuthorityManagement hljtAuthorityManagement) {
		HljtAuthorityManagement temp = hljtAuthorityManagementDao.authorityManagementByTeacherId(hljtAuthorityManagement);
		if( null !=temp && null !=temp.getTeacherStrengthSort()) {
			temp.setTeacherExhibition(true);
		}
		return temp;
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public Integer updateTeacherByTeacherId(HljtAuthorityManagement hljtAuthorityManagement) {
		if(null !=hljtAuthorityManagement.getTeacherStrengthSort()&&hljtAuthorityManagement.isTeacherExhibition() 
				&& null==hljtAuthorityManagementDao.findExcellentByTeacherId(hljtAuthorityManagement.getTeacherId())) {
			hljtAuthorityManagementDao.addExcellent(hljtAuthorityManagement);
		}
		return hljtAuthorityManagementDao.updateTeacherByTeacherId(hljtAuthorityManagement);
	}

	@Override
	@Transactional(readOnly=true)
	public Map<String , Object> allAuthorityManagement(HljtAuthorityManagement hljtAuthorityManagement) {
		Map<String , Object> map = new HashMap<String, Object>();
		List<HljtAuthorityManagement> list = hljtAuthorityManagementDao.allAuthorityManagement(hljtAuthorityManagement);
		for (HljtAuthorityManagement h : list) {
			if(null !=h.getTeacherStrengthSort()) {
				h.setTeacherExhibition(true);
			}
		}
		map.put("list", list);
		map.put("total", hljtAuthorityManagementDao.allAuthorityManagementConut(hljtAuthorityManagement));
		return map;
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public Integer updateTeacherByTeacherIdPer(HljtAuthorityManagement hljtAuthorityManagement) {
		return hljtAuthorityManagementDao.updateTeacherByTeacherIdPer(hljtAuthorityManagement);
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public boolean deletetTeacherAuth(HljtAuthorityManagement hljtAuthorityManagement) {
		Integer teacherId = hljtAuthorityManagement.getTeacherId();
		if(hljtAuthorityManagement.isTeacherExhibition()) {
			if(hljtAuthorityManagementDao.deletetExcellentByTeacherId(teacherId)<1) {
				return false;
			}
		}
		if(hljtAuthorityManagementDao.deletetRoleById(hljtAuthorityManagement.getRoleDelId())<1) {
			return false;
		}
		if(hljtAuthorityManagementDao.deletetTeacherById(teacherId)<1) {
			return false;
		}
		if(hljtAuthorityManagementDao.deletetUserByTeacherId(teacherId)<1) {
			return false;
		}
		return true;
	}

	
	
}
